package com.woyou.service.location;


import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EBean.Scope;
import org.androidannotations.annotations.RootContext;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

import android.content.Context;
import android.location.Location;

/**
 * 提供定位服务的实现
 * @author longtao.li
 *
 */
@EBean(scope = Scope.Singleton)
public class BaiduLocationService implements LocationServiceInterface{

	private static final String TAG = "LocationUtilBaidu";
	
//	private static final int  mLocationUpdateMinTime = 20000;  //定位请求间隔
	
	private LocationClient mLocationClient = null;
	private BDLocationListener mBDLocationListener;
	public Location location;
	private String tempcoor="gcj02"; //坐标系
	private LocationMode tempMode = LocationMode.Hight_Accuracy;//定位模式
	
	@RootContext Context mContext;

	@AfterInject
	public void init() {
		mLocationClient = new LocationClient(mContext);
//		mBDLocationListener = new MyLocationListenner();
//		mLocationClient.registerLocationListener(mBDLocationListener);
		setOption();
		
	}
	
	/**
	 * 设置定位完成后的监听器
	 * tanglc
	 * 下午8:21:46
	 * @param listener
	 */
	public void registerLocationListenner(BDLocationListener listener){
		this.mBDLocationListener = listener;
		mLocationClient.registerLocationListener(mBDLocationListener);
	}
	
	public void unRegisterLocationListener(BDLocationListener listener){
		try {
			mLocationClient.unRegisterLocationListener(listener);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	
	/**
	 * 获得最后一次定位结果
	 * @return 
	 */
	public BDLocation getLastLocation(){
		return mLocationClient.getLastKnownLocation();
	}

	//配置定位参数
    private void setOption() { 
    	LocationClientOption option = new LocationClientOption();
		option.setLocationMode(tempMode);//设置定位模式
		option.setCoorType(tempcoor);//返回的定位结果是百度经纬度，默认值gcj02
//		option.setScanSpan(mLocationUpdateMinTime);//设置发起定位请求的间隔时间,不设置就只定位一次
		option.setIsNeedAddress(true); //是否反地理编码
		mLocationClient.setLocOption(option);
	}

	@Override
	public void startLocation() {
		if (mLocationClient != null){
			mLocationClient.start();
			mLocationClient.requestLocation();
		}
	}
	

	@Override
	public void stop() {
		if( mLocationClient != null && mLocationClient.isStarted() ){
			mLocationClient.stop();
		}
		
	}
	
	/**
	 * 定位成功事件
	 * @return
	 */
//	@Produce public EventLocation<BDLocation> eventLocation(){
//		return new EventLocation<BDLocation>(mLocation);
//	}
	
	private BDLocation mLocation;
	/**
	 * 实现实位回调监听
	 */
//	public class MyLocationListenner implements BDLocationListener {
//		
//		@Override
//		public void onReceiveLocation(BDLocation location) {
//			//Receive Location 
//			StringBuffer sb = new StringBuffer(256);
//			sb.append("time : ");
//			sb.append(location.getTime());
//			sb.append("\nerror code : ");
//			sb.append(location.getLocType());
//			sb.append("\nlatitude : ");
//			sb.append(location.getLatitude());
//			sb.append("\nlontitude : ");
//			sb.append(location.getLongitude());
//			sb.append("\nradius : ");
//			sb.append(location.getRadius());
//			if (location.getLocType() == BDLocation.TypeGpsLocation){
//				sb.append("\nspeed : ");
//				sb.append(location.getSpeed());
//				sb.append("\nsatellite : ");
//				sb.append(location.getSatelliteNumber());
//				sb.append("\ndirection : ");
//				sb.append("\naddr : ");
//				sb.append(location.getAddrStr());
//				sb.append(location.getDirection());
//			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
//				sb.append("\naddr : ");
//				sb.append(location.getAddrStr());
//				//运营商信息
//				sb.append("\noperationers : ");
//				sb.append(location.getOperators());
//			}
//			LogUtil.i(TAG, sb.toString());
//			if( location != null ){
//				mLocation = location;
//				BusProvider.getInstance().post(eventLocation());
//			}
//			
//			
//		}
//		
//	}
	

	
}
